12. Exercise: Style your Notification

L1 A10 Style Your Notification

Android Developer Documentation

Exercise

  1. Open NotificationUtils.kt and find the sendNotification function.



  2. Start with loading the image from resources by using the BitmapFactory.
// TODO: Step 2.0 add style
val eggImage = BitmapFactory.decodeResource(
     applicationContext.resources, 
     R.drawable.cooked_egg
)
  1. Once you have the image, create a new BigPictureStyle and set your image.



  2. You also need to set the bigLargeIcon() to null so the large icon goes away when the notification is expanded.
// NotificationUtils.kt

// TODO: Step 2.0 add style
val eggImage = BitmapFactory.decodeResource(
     applicationContext.resources, 
     R.drawable.cooked_egg
)
val bigPicStyle = NotificationCompat.BigPictureStyle()
        .bigPicture(eggImage)
        .bigLargeIcon(null)
  1. Set the new style to notificationBuilder by calling setStyle() on the builder object.



  2. Set your image as setLargeIcon on the builder so the image will be displayed as a smaller icon when notification is collapsed.
// NotificationUtils.kt
// TODO: Step 2.1 add style to builder
.setStyle(bigPicStyle)
.setLargeIcon(eggImage)
  1. Run the app and set a timer. When the notification is first shown, it is in the collapsed state in the notification drawer. If you expand the notification, a large image is shown in the extended notification area.